package designPattern.singleton;

public class TestSingleton {

	public static int number = 0;

	public static void main(String[] args) throws Exception{

		TestSingleton obj = new TestSingleton();
		obj.testUnsafeSingleton();
		obj.testSafeSingleton();
		obj.testDoubleCheckSingleton();
		obj.testDemanHolderSingleton();
		
	}
	
	
    void testUnsafeSingleton() throws Exception{
    	
    	
    	int threadCount = 2;
   
		for(int i = 0; i < threadCount; i++){

			Thread thread = (new Thread(new TestUnsafeThreadRunnable()));
			thread.start();
		}
		    
    }
	
     void testSafeSingleton() throws Exception{
    	
    	
    	int threadCount = 2;
   
		for(int i = 0; i < threadCount; i++){

			Thread thread = (new Thread(new TestSafeThreadRunnable()));
			thread.start();
		}
		    
    }
     
     void testDoubleCheckSingleton() throws Exception{
     	
     	
     	int threadCount = 2;
    
 		for(int i = 0; i < threadCount; i++){

 			Thread thread = (new Thread(new TestDoubleCheckSingletonRunnable()));
 			thread.start();
 		}
 		    
     }
     
     void testDemanHolderSingleton() throws Exception{
      	
      	
      	int threadCount = 2;
     
  		for(int i = 0; i < threadCount; i++){

  			Thread thread = (new Thread(new TestDemandHolderSingletonRunnable()));
  			thread.start();
  		}
  		    
      }
	
	
	
	class TestUnsafeThreadRunnable implements Runnable{

		@Override
		public void run()  {

			try {
				ThreadUnsafeSingleton.getInstance();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	class TestSafeThreadRunnable implements Runnable{

		@Override
		public void run()  {

			try {
				ThreadSafeSingleton.getInstance();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	class TestDoubleCheckSingletonRunnable implements Runnable{

		@Override
		public void run()  {

			try {
				DoubleCheckSingleton.getInstance();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	class TestDemandHolderSingletonRunnable implements Runnable{

		@Override
		public void run()  {

			try {
				DemandOnHolderSingleton.getInstance();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
}
